package com.dhecp.project.api.controller;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.project.api.domain.ApiResponse;
import com.dhecp.project.api.domain.FbaPlanOrder;
import com.dhecp.project.api.service.ApiFbaInboundService;
import com.dhecp.project.api.service.ApiLogService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.Map;

/**
 * @ClassName ApiFbaInboundController
 * @Description 货件
 * @Author fxz
 * @Date 2022-12-13 21:47
 */
@Controller
@RequestMapping("/api/fbaInbound")
public class ApiFbaInboundController extends BaseController {

    //头程下单
    private static final String ADD_PARCEL = "addParcel";

    @Autowired
    private ApiFbaInboundService apiFbaInboundService;

    @Autowired
    private ApiLogService apiLogService;


    /**
     * 头程下单(手动创建货件)
     */
    @RequestMapping("/addParcel")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse addParcel(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.addParcel(params);
        return apiResponse;
    }

    /**
     * 头程下单(自动创建货件)
     */
    @RequestMapping("/addParcelZD")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse addParcelZD(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.addParcelZD(params);
        if (apiResponse.getCode() == 500){
            apiLogService.addApiLog("api","AddParcel","body：" + params.get("infoItems").toString(),"",500, apiResponse.getMsg() +" ResponseBody=" +apiResponse.getMessage());
        }
        return apiResponse;
    }

    /**
     * 头程货件回退
     */
    @RequestMapping("/cancelParcel")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse cancelParcel(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.cancelParcel(params);
        return apiResponse;
    }


    /**
     * 头程货件上传箱规
     */
    @RequestMapping("/placeOrder")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse placeOrder(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.placeOrder(params);
        return apiResponse;
    }


/**
     * 头程装箱自动上传箱规
     */
    @RequestMapping("/placeOrderZd")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse placeOrderZd(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.placeOrderZd(params);
        return apiResponse;
    }

    /**
     * 头程货件修改或者删除
     */
    @RequestMapping("/deleteOrModify")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse deleteOrModify(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.deleteOrModify(params);
        return apiResponse;
    }

    /**
     * 头程货件自动修改成发货状态
     */
    @RequestMapping("/zdModify")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse zdModify(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.zdModify(params);
        return apiResponse;
    }

    /**
     * 打印箱标
     */
    @RequestMapping("/getLabels")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse getLabels(@RequestParam Map<String, Object> params,HttpServletResponse response)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.getLabels(params);
        return apiResponse;
    }

    /**
     * 打印箱标尾程
     */
    @RequestMapping("/getLabelsWc")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse getLabelsWc(@RequestParam Map<String, Object> params,HttpServletResponse response)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.getLabelsWc(params);
        return apiResponse;
    }

    /**
     * 导入FBA
     */
    @RequestMapping("/fbaImport")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse fbaImport(@RequestParam Map<String, Object> params,HttpServletResponse response)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.fbaImport(params);
        return apiResponse;
    }

    /**
     * 打开pdf
     */
    @GetMapping("/openPdf")
    @ResponseBody
    @RepeatSubmit
    public void  openPdf(@RequestParam Map<String, Object> params,HttpServletResponse response)
            throws Exception {
        apiFbaInboundService.openPdf(params,response);
    }

    /**
     * 头程装箱并发货回退
     */
    @RequestMapping("/rollbackState")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse rollbackState(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.rollbackState(params);
        return apiResponse;
    }

    /**
     * 强制回退
     */
    @RequestMapping("/forcedRollback")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse forcedRollback(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.forcedRollback(params);
        return apiResponse;
    }

    /**
     * 头程完成单回退
     */
    @RequestMapping("/completeBackoff")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse completeBackoff(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setMsg("不允许回退");
        apiResponse.setCode(400);
//        ApiResponse apiResponse = apiFbaInboundService.completeBackoff(params);
        return apiResponse;
    }

    /**
     * 头程货件取消已装箱
     */
    @RequestMapping("/unbox")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse unbox(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.unbox(params);
        return apiResponse;
    }


    /**
     * 上传头程追踪编号
     */
    @RequestMapping("/uploadtraceNumber")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse uploadtraceNumber(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.uploadtraceNumber(params);
        return apiResponse;
    }

    /**
     * 海外尾程单审核
     */
    @RequestMapping("/updateStateName")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse updateStateName(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.updateStateName(params);
        return apiResponse;
    }

    /**
     * 完成单拆箱
     */
    @RequestMapping("/devanningWc")
    @ResponseBody
    @RepeatSubmit
    public ApiResponse devanningWc(@RequestParam Map<String, Object> params)
            throws Exception {
        ApiResponse apiResponse = apiFbaInboundService.devanningWc(params);
        return apiResponse;
    }
}
